const host = "https://sroomonline.17link.cc/"; //正式站
// const host = "https://sroomtest.17link.cc/"; //测试站

let logfirst = true;
// 检测微信小程序环境
if (wx) {
  wx.$ = {};
  wx.$.recommend_id = 0;
  wx.$.channel = '';
  wx.$.host = host;
  wx.$.getRequestOptions = function (options) {
    let url = options.url;
    let header = options.header || {};
    if (options.url.indexOf("https://") !== 0 && options.url.indexOf("http://") !== 0) {
      if (options.url.indexOf("?") == -1) {
        url = `${wx.$.host}${options.url}?api_token=${wx.getStorageSync("vs-token")}`;
      } else {
        url = `${wx.$.host}${options.url}&api_token=${wx.getStorageSync("vs-token")}`;
      }
    } else {
      if (options.url.indexOf(`${wx.$.host}`) !== 0) {
        url = `${wx.$.host}`;
        header.Proxy = options.url;
      }
    }

    header = Object.assign({
        "Content-Type": "application/json",
        appVersion: "1.0.0",
        deviceType: "miniprog",
        Accept: "application/json"
      },
      header
    );

    return Object.assign(options, {
      url,
      header
    });
  };
  wx.$.request = function (options) {
    return wx.request(wx.$.getRequestOptions(options));
  };
  wx.$.login = function (userInfo) {
    wx.showLoading({
      title: "登录中..."
    });
    const data = {
      recommend_id: wx.$.recommend_id,
      channel: wx.$.channel,
      code: userInfo.code,
      encryptedData: userInfo.encryptedData,
      iv: userInfo.iv
      // iv: encodeURIComponent(userInfo.iv)
    };
    wx.$.request({
      url: host + "api/member/login",
      method: "POST",
      data: data,
      success: res => {
        res = res.data;
        console.log(res)
        if (res && res.data.token) {
          wx.setStorageSync("vs-token", res.data.token);
          wx.setStorageSync("vs-info", res.data.member);
          if (res.data.member.mobile) {
            if (userInfo.typeurl) {
              wx.redirectTo({
                url: userInfo.url
              });
            } else {
              setTimeout(() => {
                wx.navigateBack();
                wx.hideLoading();
                if (userInfo.eventChannel) userInfo.eventChannel.emit("monitoringData", {
                  type: true
                });
              }, 500);
            }
          } else {
            setTimeout(() => {
              wx.navigateTo({
                url: "/pages/bindTel/bindTel",
                success: function (res) {
                  res.eventChannel.emit("TransferOfData2", {
                    url: userInfo.url,
                    typeurl: userInfo.typeurl,
                    executeType: userInfo.eventChannel
                  });
                }
              });
              wx.hideLoading();
            }, 500);
            wx.hideLoading();
          }
          wx.setStorageSync("phone", res.data.member.mobile);
        } else {
          wx.hideLoading();
          wx.showModal({
            title: "提示",
            content: "登录失败",
            confirmColor: "#50B23F",
            showCancel: false
          });
        }
      },
      fail: res => {
        console.error("wx.$.login request Fail" + res.errMsg);
      }
    });
  };
  wx.$.fetch = function (url, options = {}) {
    options.url = url;
    if (!options.hideLoading) {
      wx.showLoading({
        title: "加载中",
        mask: true,
        duration: 10000
      });
    }
    return new Promise((resolve, reject) => {
      options.success = res => {
        wx.hideLoading();
        if (res.data.code == 401 && logfirst == true) {
          wx.navigateTo({
            url: "/pages/login/login"
          });
          logfirst = false;
          setTimeout(() => {
            logfirst = true;
          }, 5000);
        } else if (res.data.state == 3 && logfirst == true) {
          wx.navigateTo({
            url: "/pages/bindTel/bindTel"
          });
          logfirst = false;
          setTimeout(() => {
            logfirst = true;
          }, 5000);
        } else {
          setTimeout(() => {
            logfirst = true;
          }, 5000);
          resolve(res);
        }
      };
      options.fail = res => {
        wx.hideLoading();
        reject(res);
      };
      wx.$.request(options);
    });
  };
  wx.$.upload = function (options) {
    return wx.uploadFile(wx.$.getRequestOptions(options));
  };
  module.exports = wx.$;
}
